<?php
defined('BASEPATH') or exit('No direct script access allowed');

class CheckPermission extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('CheckPermissionDB');
    }

    //点击编辑时校验
    public function editCheck()
    {
        $recordId = $_REQUEST['recordId'];
        $type = $_REQUEST['type'];
        $user = $this->session->user;
         $username = $user['username'];
        $result = $this->CheckPermissionDB->find(array(
            'record_id' => $recordId,
            'type' => $type
        ));
 //       $date = date('y-m-d h:i:s', time());
        $date = time();
        $replay = array();
        if (empty($result[0])) {
            $param = array('record_id' => $recordId,
                'type' => $type,
                'username' => $username,
                'is_lock' => 1,
                'lock_time' => $date);
            $this->CheckPermissionDB->insert($param);
            $replay['result'] = 0;
            $replay['data'] = '可以修改';
            echo json_encode($replay);
            return true;
        } else {
            // 如果有人修改
            if ($result[0]['is_lock'] == 1) {
                    //校验是否超时
                    $waitTime = $this->config->item('waitTime');
                    $locktime = $result[0]['lock_time'];
                    $time = time() - $locktime;
                    //echo ($time >= $waitTime * 60);
                    /*die(); */
                    if($time >= $waitTime * 60){
                        $param = array('username' => $username,'lock_time'=>time());
                        $condition = array('record_id'=>$recordId,'type' => $type);
                        $this->CheckPermissionDB->update($condition,$param);
                        $replay['result'] = 0;
                        $replay['data'] = '可以修改';
                        echo json_encode($replay);
                        return;
                    }else{
                        $replay['result'] = 1;
                        $replay['data'] = "{$result[0]['username']}正在修改，请等待";
                        echo json_encode($replay);
                        return;
                    }
            } else {
                $param = array('is_lock' => 1,'lock_time'=>time(),'username' => $username);
                $condition = array('record_id'=>$recordId,'type' => $type);
                $this->CheckPermissionDB->update($condition,$param);
                $replay['result'] = 0;
                $replay['data'] = '可以修改';
               echo json_encode($replay);
               return;
            }
        }
    }
    
    //解锁
    public function unlock(){
        $recordId = $_REQUEST['recordId'];
        $type = $_REQUEST['type'];
        $result = $this->CheckPermissionDB->find(array(
            'record_id' => $recordId,
            'type' => $type
        ));
        $user = $this->session->user;
        $username = $user['username'];
        if($result[0]['username'] != $username){
            return;
        }
        $param = array('is_lock' => 0);
        $condition = array('record_id'=>$recordId,'type' => $type);
        $this->CheckPermissionDB->update($condition,$param);
    }
    
    //点击确定时提交校验
    public function submitCheck(){
        $recordId = $_REQUEST['recordId'];
        $type = $_REQUEST['type'];
        $waitTime = $this->config->item('waitTime');
        $result = $this->CheckPermissionDB->find(array(
            'record_id' => $recordId,
            'type' => $type
        ));
        $lock = $result[0]['is_lock'];
        $username = $result[0]['username'];
        $user = $this->session->user;
        $myName = $user['username'];
        if($lock == 1){
            if($myName == $username){
                //return true;
                $condition = array('record_id'=>$recordId,'type' => $type);
                $param = array('is_lock' => 0);
                $this->CheckPermissionDB->update($condition,$param);
                echo json_encode(array("result"=>0,"data"=>"保存成功"));
                return;
            }else{
                //return false;
                echo json_encode(array("result"=>1,"data"=>"修改超时，保存不成功"));
                return;
            }
        }else{
            //return false;
            echo json_encode(array("result"=>1,"data"=>"修改超时，保存不成功"));
            return;
        }
    }
}
?>